#include "StdAfx.h"
#include "AIPlayer.h"
#include "Game.h"
#include "AIRandomPlayer.h"


AIRandomPlayer::AIRandomPlayer(void){ turnCount = 0;}

AIRandomPlayer::~AIRandomPlayer(void){ turnCount = 0;}

void AIRandomPlayer::play(sf::RenderWindow & window, Piece *piece, std::vector<Piece> &m_pieces)
{
	Location *loc;
	if(Game::thereArePiecesLeft())
	{
		/*if((((*piece).getLocation()).first==4) && (((*piece).getLocation()).second==4))
		{*/
			//malloc the new location...
			loc = (Location *)malloc(sizeof(Location));
			srand((unsigned)time(0));
			do
			{	
				//select a free space on the board	
				loc->first = rand()%4;
				loc->second = rand()%4;
			}
			while(Game::thereIsPiece(*loc));
			//setting the new location for the piece
			(*piece).setLocation(*loc);
			this->increaseTurnCount();
		//}

	}
	return;
}

Piece *AIRandomPlayer::choosePiece(sf::RenderWindow & window, std::vector<Piece> &m_pieces)
{
	Piece *piece;
	srand((unsigned)time(0));
	//i take one random piece that's not on the board
	do
	{
		piece = &m_pieces[rand()%16];
	}
	while((*piece).getLocation().first !=16 || (*piece).getLocation().second !=16);
	piece->setLocation(std::make_pair(4,4));
	return piece;
}